package br.com.vemtransporte.conexao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

public class Conexao {
	private static final String CATEGORIA = "teste";
	private static final String URLCategoria = "";
  	private static final String URLEstabelecimento  = "";
  	private static final String URLEndereco = "";
  	private static final String URLSugestao = "";
  	private static List<String> categorias = new ArrayList<String>();
  	private static List<String> lista = new ArrayList<String>();
  	private static String endereco;
  	private static String retornoSugestao;
  	private static String[] estabelecimentos;
  	
  	public static List<String> buscarCategoria() {
  		try{
  			URL myURL = new URL(URLCategoria);
	       	URLConnection ucon = myURL.openConnection();
               
            InputStream in = ucon.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            categorias.clear();    
			String line = null;
            while ((line = reader.readLine()) != null) {
		        categorias.add(line); 
			}
		} catch (IOException ioe) {
			Log.i(CATEGORIA, "IOException " + ioe);
		}		
  		return categorias;
  	}

	public static String buscarEndereco(String nome) {
		try{
			nome = nome.replace(" ","%20");
			URL myURL = new URL(URLEndereco + "?nome=" + nome);	
			Log.i(CATEGORIA, "URLEndereco " + myURL);
			URLConnection ucon = myURL.openConnection();
           
			InputStream in = ucon.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            
			endereco = reader.readLine();
			
		} catch (IOException ioe) {
			Log.i(CATEGORIA, "IOException " + ioe);
		}
		return endereco;		
	}

	public static String[] buscarEstabelecimento(String cat) {
		try{
			cat = cat.replace(" ","%20");
			URL myURL = new URL(URLEstabelecimento + "?categoria=" + cat);
			URLConnection ucon = myURL.openConnection();
           
			InputStream in = ucon.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			lista.clear();
			String line = null;
			while ((line = reader.readLine()) != null) {
				lista.add(line);
			}  
			estabelecimentos = (String[]) lista.toArray (new String[lista.size()]); 
		} catch (IOException ioe) {
			Log.i(CATEGORIA, "IOException " + ioe);
		}
		return estabelecimentos;
	}
	
	public static String gravarSugestao(String nome, String email, String descricao) {
		try{
			nome = nome.replace(" ","%20");
			email = email.replace(" ","%20");
			descricao = descricao.replace(" ","%20");
			URL myURL = new URL(URLSugestao + "?nome=" + nome + "&email=" + email + "&descricao=" + descricao);
			URLConnection ucon = myURL.openConnection();
			
			InputStream in = ucon.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            
			retornoSugestao = reader.readLine();
		}
    	catch(Exception ex){
    		Log.i(CATEGORIA, "Exception " + ex);
    	}
    	return retornoSugestao;    	
	}
}